package machine.send.service.impl;

import cn.hutool.json.JSONUtil;
import machine.api.ChatAIApi;
import machine.api.SetuApi;
import machine.api.WallpaperApi;
import machine.api.WeatherApi;
import machine.bean.MsgType;
import machine.bean.Setu;
import machine.bean.Weather;
import machine.send.service.PrepareSendMsgService;
import machine.utils.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhanghui145
 * @date 2021/12/24 17:50
 * @since 1.0.0
 */
@Service
public class PrepareSendMsgServiceImpl implements PrepareSendMsgService {

    @Autowired
    private WeatherApi weatherApi;

    @Autowired
    private SetuApi setuApi;

    @Autowired
    private WallpaperApi wallpaperApi;

    @Autowired
    private CacheUtils cacheUtils;

    @Autowired
    private ChatAIApi chatAIApi;

    @Override
    public boolean msgCheck(String type, String msg) {
        // "message":"[CQ:at,qq=2085801530] 顺丰到付"
        if (MsgType.GROUP.getType().equals(type) && msg.startsWith("[CQ:at,qq=721465017]")
                && (msg.contains("天气查询") || msg.contains("色图") || msg.contains("壁纸"))) {
            return true;
        } else return MsgType.PRIVATE.getType().equals(type);
    }

    @Override
    public String prepareMsg(Integer id, String msg) {
        if (msg.contains("天气查询")) {
            String cityName;
            if (msg.startsWith("[CQ:at,qq=721465017]")) {
                cityName = msg.substring(26);
            } else {
                cityName = msg.substring(5);
            }
            Weather weather = weatherApi.getWeatherByLocation(cityName);
            return weatherMsg(cityName, weather);
        }
        if (msg.contains("色图")) {
            return setuMsg(setuApi.getPicture());
        }
        if (msg.contains("壁纸")) {
            String url = wallpaperApi.randomWallpaper();
            return "[CQ:image,file="+ url +"]";
        }
        if (msg.contains("开始聊天")) {
            cacheUtils.put(String.valueOf(id));
            return "好的，现在开始聊天~";
        }
        if (msg.contains("结束聊天")) {
            cacheUtils.remove(String.valueOf(id));
            return "好的~";
        }
        if (cacheUtils.contain(String.valueOf(id))) {
            return chatAIApi.replyMsg(msg);
        }
        return null;
    }

    private String weatherMsg(String cityName, Weather weather) {
        return cityName + "天气，温度：" + weather.getNow().getTemp()
                + "°，体感温度：" + weather.getNow().getFeelsLike() + "°，天气状况："
                + weather.getNow().getText() + "，风向：" + weather.getNow().getWindDir()
                + "，风力等级：" + weather.getNow().getWindScale() + "，云量："
                + weather.getNow().getCloud() + "%，更多信息：" + weather.getFxLink();
    }

    private String setuMsg(Setu setu){
        String originalUrl = setu.getUrls().get(0);
        String url = (String)JSONUtil.parseObj(originalUrl).get("original");
        return "作者pid：" + setu.getPid() + "\n作者uid：" + setu.getUid() +
                "\n标题：" + setu.getTitle() + "\n是否R18：" + (!setu.isR18() ? "否":"是") +
                "\n标签：" + setu.getTags() + "\n地址：" + url;
    }
}
